package android.medicalcloud.com.yiapp.yilian.utils.net;



import android.graphics.YuvImage;
import android.medicalcloud.com.yiapp.base.cach.PreferceManager;
import android.medicalcloud.com.yiapp.base.net.lisener.NetCallback;
import android.medicalcloud.com.yiapp.base.net.lisener.ViewNetCallBack;
import android.medicalcloud.com.yiapp.base.utils.Logger;
import android.medicalcloud.com.yiapp.base.utils.MD5Util;
import android.medicalcloud.com.yiapp.base.utils.MapUtils;
import android.medicalcloud.com.yiapp.base.utils.StringTools;
import android.medicalcloud.com.yiapp.yilian.application.YilianAppcation;
import android.medicalcloud.com.yiapp.yilian.configer.enums.HttpConfig;
import android.medicalcloud.com.yiapp.yilian.configer.xml.HttpManager;
import android.medicalcloud.com.yiapp.yilian.model.HttpConfigBean;
import android.medicalcloud.com.yiapp.yilian.utils.android.GsonTool;
import android.medicalcloud.com.yiapp.yilian.utils.android.IntentTool;
import android.medicalcloud.com.yiapp.yilian.utils.android.JsonTool;

import org.json.JSONObject;

import java.io.Serializable;
import java.util.Map;

/**
 * 对底层接口的 对接
 */
public class BaseNetImpl implements NetCallback {

    ViewNetCallBack listener;
    Class entityClass;
    HttpConfigBean config;
    Map<String, Object> param;
    HttpConfig con;

    public BaseNetImpl(ViewNetCallBack listener, Class entityClass, HttpConfig config) {
        this.listener = listener;
        this.entityClass = entityClass;
        this.config = HttpManager.getInstance().getConifgById(config);
        con = config;

    }

    public void setParam(Map<String, Object> param) {
        this.param = param;
    }

    @Override
    public void dealReslut(final String jsonstring) {
    	
    	
    	
    	
    	
        try {
            String code  = JsonTool.getString(jsonstring, "code");
//            code ="40001";
            if(StringTools.isNullOrEmpty(code)){
            	 String url = MapUtils.map2UrlParams(config.getActioin(), param);
                 Logger.e(url + "baseResult=" + jsonstring);
                 
                 if (config.isCach()) {
                     url = MapUtils.map2UrlParams(config.getActioin(), param);
                     PreferceManager.getInsance().saveValueBYkey(MD5Util.string2MD5(url), jsonstring);
                 }

                 boolean bool = JsonTool.isArray(jsonstring);
                 if (bool) {
                     listener.onData((Serializable) GsonTool.jsonToArrayEntity(jsonstring, entityClass), config.getId(), true, jsonstring);
                     return;
                 }


                 try {
                     listener.onData((Serializable) GsonTool.jsonToEntity(jsonstring, entityClass), config.getId(), true, jsonstring);
                 } catch (Exception e) {
                     listener.onData((Serializable) GsonTool.jsonToArrayEntity(jsonstring, entityClass), config.getId(), true, jsonstring);
                 }

                 
            }else{
//            	IntentTool.loginClearTop(YiApplication.getInstance().getApplicationContext());
            }
           
        } catch (Exception e) {
            listener.onFail(e, con, "");
        }
    }

    @Override
    public void dealFailer(Exception e) {
        if (config.isCach()) {
            String url = MapUtils.map2UrlParams(config.getActioin(), param);
            try {
                String jsonstring = PreferceManager.getInsance().getValueBYkey(MD5Util.string2MD5(url));
                JSONObject jsonObject = new JSONObject(jsonstring);

                try {
                    listener.onData((Serializable) GsonTool.jsonToEntity(jsonstring, entityClass), config.getId(), false, jsonstring);
                } catch (Exception e2) {
                    listener.onData((Serializable) GsonTool.jsonToArrayEntity(jsonstring, entityClass), config.getId(), false, jsonstring);
                }
                return;
            } catch (Exception e1) {

            }
        }
        listener.onFail(e, con, "");
    }

    @Override
    public void onstart() {

        listener.onConnectStart(con);
        String url = MapUtils.map2UrlParams(config.getActioin(), param);
        String jsonstring = PreferceManager.getInsance().getValueBYkey(MD5Util.string2MD5(url));
        if (!StringTools.isNullOrEmpty(jsonstring) && config.isCach()) {
            try {
                JSONObject jsonObject = new JSONObject(jsonstring);
                if (jsonObject.has("data")) {
                    String data = jsonObject.getString("data");
                    try {
                        listener.onData((Serializable) GsonTool.jsonToEntity(data, entityClass), config.getId(), false, jsonstring);
                    } catch (Exception e) {
                        listener.onData((Serializable) GsonTool.jsonToArrayEntity(data, entityClass), config.getId(), false, jsonstring);
                    }
                } else {
                    listener.onData(null, config.getId(), false, jsonstring);
                }
            } catch (Exception e) {

            }
        }

        try {
            if (config.isShowLoadDialog()){
//                com.yi.android.android.app.view.dialog.LoadDialog.createDialog(YilianAppcation.instance.currentAc).show();
            }
        }catch (Exception e){

        }


    }

    @Override
    public void onEnd() {
        listener.onConnectEnd();

        try {
            if (config.isShowLoadDialog()){
//                com.yi.android.android.app.view.dialog.LoadDialog.createDialog(YiApplication.getInstance().currentAc).dismiss();
            }
        }catch (Exception e){

        }
    }


}
